// packageF/sachertorte/deliveryArea/deliveryArea.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsId:'',
    city:'',
    longitude:'',
    latitude:'',
    shipAreaType:'',
    storeList:[
    ],
    markers_2: [{ // 获取返回结果，放到mks数组中
      latitude: 23.099994,
      longitude: 113.324520,
      iconPath: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png", //图标路径
      width: 20,
      height: 25,
      callout: {
        padding: '10rpx',
        borderRadius: '5rpx'
      }
    }],
    circle: [{
      latitude:'',
      longitude: '',
      radius:'',
      fillColor: "#FF88887F",
      color: "#FFF",
      strokeWidth: 2,
      zIndex: 1
    }],
    polygons_2: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.goodsId) {
      this.setData({
        goodsId: options.goodsId,
        city: options.city
      })
    }
    console.log(typeof this.data.longitude);
    this.getRule();
  },
  getRule() {
    let query = {
      city:this.data.city,
      goods_id: this.data.goodsId,
      // cake_ceshi:1 //测试环境使用
    };
    let urlStr = app.getNetAddresss("plugin.yz-supply-cake.frontend.index.index.getRule");
    app._getNetWork({
      url: urlStr,
      data:query,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          console.log(res.data);
          if(res.data.code !== 200) {
            wx.showToast({
              icon: 'none',
              title: res.data.msg,
              duration: 1500
            });
            this.setData({
              description:res.data.msg,
            })
            return;
          }
          let data = res.data;
          let shipAreaType = data.data?.ship_area_type;
          let shopList = data.data?.shop || [];
          if(shipAreaType === '1' || shipAreaType === 1) {//地图区域)
            let shipAreas = JSON.parse(data.data?.ship_areas) || {};
            let areas = shipAreas.areas || []; 
            console.log(areas)
            let arr = [];
            let pointsArr = [];
            areas.map((item,index) => {
              let points = item.points;
              pointsArr.push({
                points:[]
              })
              for(let i in points) {
                pointsArr[index].points.push({
                  latitude : points[i].lat,
                  longitude: points[i].lng
                });
              }
                arr.push({
                  points: pointsArr[index].points,
                  fillColor: "#FF88887F",
                  strokeColor: "#FFF",
                  strokeWidth: 2,
                  zIndex: 1
                })
            });
            this.setData({
              polygons_2:arr,
              latitude:arr[0]?.points[0]?.latitude,
              longitude:arr[0]?.points[0]?.longitude
            })
          }
          
          if(shipAreaType === '2' || shipAreaType === 2) {//门店半径区域)
            let shipRule = JSON.parse(data.data?.ship_rule) || {};
            console.log(shipRule);
            let nonDefault = shipRule.non_default || {}; 
            let distance = '';
            for(let key in nonDefault) {
              console.log(key);
              distance = nonDefault[key].distance;
            }
            this.setData({
              "circle[0].longitude": shopList[0]?.lng,
              "circle[0].latitude": shopList[0]?.lat,
              "circle[0].radius": distance * 1000,
            })
          }
          this.setData({
            storeList:shopList,
            "markers_2[0].longitude": shopList[0]?.lng,
            "markers_2[0].latitude": shopList[0]?.lat,
            shipAreaType:shipAreaType
          })
          console.log(this.data.shipAreaType);
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})